/*
 * @Description: http错误
 * @Author: iamsmiling
 * @Date: 2021-06-16 06:56:36
 * @LastEditTime: 2021-06-16 06:58:41
 */

import 'package:connectivity/connectivity.dart';
import 'package:credit_business_circle/http_kit/exception/offline_exception.dart';
import 'package:credit_business_circle/http_kit/exception/server_exception.dart';
import 'package:dio/dio.dart';

class HttpErrorInterceptor extends InterceptorsWrapper {
  @override
  onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    //没有网络
    var connectivityResult = await (new Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.none) {
      return handler.reject(OfflineException("网络开小差了，点击屏幕重新加载~", options));
    }
    handler.next(options);
  }

  @override
  onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response.statusCode == 500) {
      return handler.reject(ServerException("服务器异常", response.requestOptions));
    }

    ///...自定义异常处理
  }
}
